//
//  OilOrderViewController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/7/11.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "OilOrderViewController.h"
#import "OilOrderRootFirstVC.h"
#import "OilOrderRootSecondVC.h"
#import "OilOrderRootThirdVC.h"
#import "OilOrderRootFourVC.h"
#import "OilOrderRootZeroVC.h"

#import "DrawbackController.h"
#import "MineTableViewController.h"
#import "OrderPaymentController.h"

#define TOPVIEW_HEIGHT 36
#define SLIDEVIEW_HEIGHT 3

@interface OilOrderViewController ()<UIScrollViewDelegate>
{
    UIView *_topView;
    UIView *_slideView;
    UIScrollView *_bottomScrollView;
    CGRect _bottomScrollViewFrame;
    UIButton *_selectedButton;
    MBProgressHUD *progressHUD;
}

@property(nonatomic,strong) NSMutableArray *btnArray;


@property(nonatomic,strong)UITableView *goodsOrderView;

@property(nonatomic,strong)NSArray *vcArray;

//控制器下标索引
@property(nonatomic)NSInteger scrollerIndex;

@end

@implementation OilOrderViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"油卡订单";
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self buildGoodsOrderView];
    
    //左边
    
    UIBarButtonItem *backItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"jiantou"] highImage:[UIImage imageNamed:@"jiantou"] target:self action:@selector(backToMine) forControlEvents:UIControlEventTouchUpInside title:nil];
    self.navigationItem.leftBarButtonItem = backItem;
    
    //申请退款
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(oilpushToDarwBackNotification:) name:@"coastPushToDrawbackVC" object:nil];
    //支付订单
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(oilpushToPayOrderNotification:) name:@"coastPushToPay" object:nil];
}


- (void)oilpushToDarwBackNotification:(NSNotification *)notification
{
    DrawbackController *DrawbackVC = [[DrawbackController alloc] init];
    DrawbackVC.oidStr = notification.userInfo[@"oid"];
    DrawbackVC.rcMoney = notification.userInfo[@"pay_amount"];
    DrawbackVC.type = @"2";
    [self.navigationController pushForm:self.navigationController pushToAndHidden:DrawbackVC];
}

- (void)oilpushToPayOrderNotification:(NSNotification *)notification
{
    OrderPaymentController *OrderPaymentVC = [[OrderPaymentController alloc] init];
    
    OrderPaymentVC.OrderString = notification.userInfo[@"oid"];;
    OrderPaymentVC.OrderTitleString = @"油卡充值";
//    OrderPaymentVC.OrderDescribeInfo = self.oilNumberString;
    OrderPaymentVC.OrderMonryString = notification.userInfo[@"money"];
    OrderPaymentVC.judgeOilRecharge = YES;
    OrderPaymentVC.orderType = @"2";
    [self.navigationController pushForm:self.navigationController pushToAndHidden:OrderPaymentVC];
}


- (void)backToMine
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
- (void)buildGoodsOrderView
{
    CGFloat btnWidth = WIDTH/self.vcArray.count;
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0,10,WIDTH,TOPVIEW_HEIGHT)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_topView];
    
    _slideView = [[UIView alloc] initWithFrame:CGRectMake(0,TOPVIEW_HEIGHT-SLIDEVIEW_HEIGHT, btnWidth,SLIDEVIEW_HEIGHT)];
    _slideView.backgroundColor = DEFAULT_COLOR;
    [_topView addSubview:_slideView];
    
    for (int i = 0; i < self.vcArray.count; ++i) {
        
        UIView *View = [[UIView alloc] initWithFrame:CGRectMake(btnWidth*i,0,btnWidth,TOPVIEW_HEIGHT-SLIDEVIEW_HEIGHT)];
        
        UIButton *btn = [[UIButton alloc] init];
         NSString * key = [self.vcArray[i] allKeys][0];
        [btn setTitle:[NSString stringWithFormat:@"%@",key] forState:UIControlStateNormal];
        [btn setFrame:CGRectMake(0,0,btnWidth,TOPVIEW_HEIGHT-SLIDEVIEW_HEIGHT)];
        
        btn.tag = i;
        
        if (i==_scrollerIndex) {
            btn.enabled = false;
            _selectedButton = btn;
        }
        
        btn.titleLabel.font = FONT(13);
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:RGB(52, 52, 52) forState:UIControlStateNormal];
        [btn setTitleColor:DEFAULT_COLOR forState:UIControlStateDisabled];
        
        [View addSubview:btn];
        [_topView addSubview:View];
        [self.btnArray addObject:btn];
        
        [btn addTarget:self action:@selector(topSelectButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    _bottomScrollViewFrame = CGRectMake(0,TOPVIEW_HEIGHT+10,WIDTH,HEIGHT-TOPVIEW_HEIGHT-10);
    _bottomScrollView= [[UIScrollView alloc] initWithFrame:_bottomScrollViewFrame];
    [self.view addSubview:_bottomScrollView];
    _bottomScrollView.contentSize = CGSizeMake(self.vcArray.count*WIDTH,0);
    _bottomScrollView.pagingEnabled = YES;
    _bottomScrollView.showsVerticalScrollIndicator = false;
    _bottomScrollView.showsHorizontalScrollIndicator = false;
    _bottomScrollView.bounces = false;
    _bottomScrollView.directionalLockEnabled = YES;
    _bottomScrollView.delegate =self;
    
    _scrollerIndex = 0;
    _bottomScrollView.contentOffset = CGPointMake(WIDTH*_scrollerIndex, 0);
    
    for (int i = 0; i < self.vcArray.count ; i ++) {
        CGRect  VCFrame = CGRectMake(i * WIDTH,10,WIDTH, _bottomScrollViewFrame.size.height-10);
        
        NSString * key = [self.vcArray[i] allKeys][0];
        UIViewController * vc = _vcArray[i][key] ;
        vc.view.frame = VCFrame;
        
        [_bottomScrollView addSubview:vc.view];
    }
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //
    CGRect frame = _slideView.frame;
    frame.origin.x = scrollView.contentOffset.x/self.vcArray.count;
    _slideView.frame = frame;
    int pageNum = scrollView.contentOffset.x / WIDTH;
    //滚动buttonLabel color
    for (UIButton * btn in self.btnArray) {
        if (btn.tag == pageNum ) {
            btn.enabled = false;
            
        }else{
            btn.enabled = YES;
        }
    }
    
}

- (void)topSelectButtonDidClick:(UIButton *)sender
{
    UIButton *button = sender;
    _selectedButton.enabled = YES;
    button.enabled = false;
    _selectedButton = button;
    [_bottomScrollView setContentOffset:CGPointMake(button.tag * WIDTH, 0) animated:YES];
}


#pragma mark >>>>>>>>>>懒加载

- (NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray = [[NSMutableArray alloc] initWithCapacity:5];
    }
    return _btnArray;
}

- (NSArray *)vcArray
{
    if (_vcArray == nil) {
        _vcArray = [[NSArray alloc] init];
        _vcArray = @[@{@"全部":[OilOrderRootZeroVC new]},@{@"未付款":[OilOrderRootFirstVC new]},@{@"已付款":[OilOrderRootSecondVC new]},@{@"已退款":[OilOrderRootThirdVC new]},@{@"成功":[OilOrderRootFourVC new]}];
    }
    return _vcArray;
}


@end
